package com.keai.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.keai.constant.MessageConst;
import com.keai.entity.Result;
import com.keai.pojo.OrderSetting;
import com.keai.service.OrderSettingService;
import com.keai.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {


    @Reference
    OrderSettingService orderSettingService;


    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            List<String[]> stringList = POIUtils.readExcel(excelFile);
//            for (String[] strings : stringList) {
//                System.out.println(Arrays.toString(strings));
//            }
            //把stringList 数据转换到List<OrderSetting>中
            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (String[] strings : stringList) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(new SimpleDateFormat("yyyy/MM/dd").parse(strings[0]));
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                //添加到集合中
                orderSettingList.add(orderSetting);
            }
            //更新数据库
            orderSettingService.editOrderSettingList(orderSettingList);

            return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
    }



    @RequestMapping("/findByMonth")
    public Result findByMonth(String month){
        try {
            List<OrderSetting> orderSettingList = orderSettingService.findByMonth(month);
            //把查询到数据转换成页面需要的数据模型
            List<Map<String,Object>> mapList = new ArrayList<>();
            for (OrderSetting orderSetting : orderSettingList) {
                //一个ordersetting对应一个Map集合
                Map<String,Object> map = new HashMap<>();
    //            map.put("date", orderSetting.getOrderDate().getDate());
                map.put("date", new SimpleDateFormat("dd").format(orderSetting.getOrderDate()));
                map.put("number",orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());
                //把map添加到集合中
                mapList.add(map);
            }
            return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS, mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }
    }
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result edit(@RequestBody OrderSetting orderSetting){
        try {
            List<OrderSetting> orderSettingList = new ArrayList<>();
            orderSettingList.add(orderSetting);

            orderSettingService.editOrderSettingList(orderSettingList);
            return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ORDERSETTING_FAIL);
        }
    }
}
